package com.hanlyjiang.library.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * Bitmap 相关工具类
 * @author jianghanghang
 *
 */
public class BitmapUtils {


    /**
     * 从Assets中获取图片资源
     *
     * @param context
     * @param filename 文件目录
     * @return 图片对象
     */
    public static Bitmap getBitmapFromAssets(Context context, String filename) {
        InputStream is = null;
        Bitmap bitmap = null;
        try {
            // 获取字节流
            is = context.getAssets().open(filename);
            // 转成图片对象
            bitmap = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    // 关闭字节流
                    is.close();
                    is = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    /** 从ResId 获得一定采样率的bitmap
     * @param res Resource对象
     * @param resId 资源id
     * @param reqWidth 期望的宽(px)
     * @param reqHeight 期望的高
     * @return
     */
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    /**从图片路径获得一定采样率的bitmap
     * @param filePath 路径
     * @param reqWidth 期望宽
     * @param reqHeight 期望高
     * @return 图片
     */
    public static Bitmap decodeSampledBitmapFromPath(String filePath,
                                                     int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * 从文件路径decode bitmap 并根据设备密度缩放
     *
     * @param filePath        文件路径
     * @param inDensity       该图片设计时适配的设备的DPI (如320)
     * @param inTargetDensity 当前设备的DPI (如240)
     * @return bitmap
     */
    public static Bitmap decodeScaledBitmapFromPath(String filePath, int inDensity, int inTargetDensity) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inDensity = inDensity;
        options.inTargetDensity = inTargetDensity;
        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * 计算采样缩放size -  power of 2 (2 - 图像被缩至1/2)
     *
     * @param options   inJustDecodeBounds = false 时使用BitmapFactory.decode方法decode后的option参数
     * @param reqWidth  期望width
     * @param reqHeight 期望height
     * @return 采样缩放size（power of 2 ）
     */
    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

}
